﻿using NDK.Module.BLL;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.Monitor;
using NDK.Motion.Sys;
using NDK.Utils;
using Hervey;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static NDK.Module.Model.Common;
using NDK.LocalUtils;
using NDK.Motion.MachineResources;
using NPOI.SS.Formula.Functions;
using NDK.Module.Model.Quebec;

namespace NDK.Module.StationLogic
{
    [Monitor(Description = "测量1工位")]
    public class Measure1 : StationLogicBase
    {
        #region 通用
        //记录当前步骤，用于步骤变化的比较
        private StationStep _curStep = StationStep.WaitMacInit;
        //对外提供工位的步骤变化事件
        public override event PropertyChangedEventHandler PropertyChanged;
        //对外提供唯一实例
        public static Measure1 Instance = new Measure1();
        //私有化的构造函数
        private Measure1() : base()
        {
            this.Name = "测量1";
        }
        //步骤字段、属性
        private StationStep _step = StationStep.WaitMacInit;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _step;
            }
            set
            {
                if (_curStep != value)
                {
                    _step = value;
                    _curStep = value;

                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        #region 变量
        [Monitor(Description = "料来源是否CarrierA")]
        public bool isMaterialFromA = true;
        [Monitor(Description = "通知上料可到测量位")]
        public bool 通知上料可到测量位 = true;
        [Monitor(Description = "是否允许载具到R角激光扫描位")]
        public bool 是否允许载具到R角激光扫描位 = true;
        [Monitor(Description = "通知中转CarrierA可下料")]
        public bool 通知中转CarrierA可下料 = false;
        [Monitor(Description = "通知中转CarrierB可下料")]
        public bool 通知中转CarrierB可下料 = false;

        [Monitor(Description = "激光组是否忙碌")]
        public bool 激光组是否忙碌 = false;
        [Monitor(Description = "R角激光是否忙碌")]
        public bool R角激光是否忙碌 = false;
        [Monitor(Description = "上相机是否忙碌")]
        public bool 上相机是否忙碌 = false;

        #endregion
        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init()
        {
            base.Init();
            Step = StationStep.WaitMacInit;
            logBaseInfo = "Measure1";

            //变量
            isMaterialFromA = true;
            通知上料可到测量位 = true;
            是否允许载具到R角激光扫描位 = true;
            通知中转CarrierA可下料 = false;
            通知中转CarrierB可下料 = false;
            激光组是否忙碌 = false;
            R角激光是否忙碌 = false;
            上相机是否忙碌 = false;
        }

        /// <summary>
        /// 工站动作流程
        /// </summary>
        /// <param name="mode"></param>
        public override void ActionProcess()
        {
            try
            {
                switch (Step)
                {
                    case StationStep.WaitMacInit:

                        Step = StationStep.Start;
                        break;
                    case StationStep.Start:
                        logCtStr = string.Empty;
                        swStepTime.Restart();
                        swStationCT.Restart();

                        logCtStr += $"{logBaseInfo},Loading工站开始,{swStepTime.ElapsedMilliseconds},{Environment.NewLine}";
                        swStepTime.Restart();
                        Step = StationStep.End;
                        break;
                    //判断1还是2接料
                    //1先
                    //给上料机械手 工位1可放料
                    //等待工位1放料完成
                    //打开真空吸
                    case StationStep.End:
                        logCtStr += $"{logBaseInfo},Loading结束,{swStepTime.ElapsedMilliseconds},{Environment.NewLine}";
                        logCtStr += $"{logBaseInfo},Loading结束,{swStationCT.ElapsedMilliseconds},{Environment.NewLine}";
                        PublicMethod.WriteLogToTxt(logCtStr, $"000StepCT_{DateTime.Now.ToString("yyyyMMdd")}", "1.Loading");
                        Step = StationStep.WaitMacInit;
                        break;
                    default:
                        break;

                }

            }
            catch (Exception ex)
            {
                UILog_Error($"[ Loading Thread ERROR] {ex.Message}");
            }
        }

        public override void EmptyActionProcess()
        {
            try
            {
                switch (Step)
                {
                    case StationStep.WaitMacInit:
                        Step = StationStep.Start;
                        break;
                    case StationStep.Start:

                        logCtStr = string.Empty;
                        swStepTime.Restart();
                        swStationCT.Restart();

                        logCtStr += $"{logBaseInfo},Measure1工站开始,{swStepTime.ElapsedMilliseconds},{Environment.NewLine}";
                        swStepTime.Restart();
                        Step = StationStep.等待通知上料完成;
                        break;
                    case StationStep.等待通知上料完成:
                        if (Loading.Instance.CarrierA通知可测量 || Loading.Instance.CarrierB通知可测量)
                        {
                            if (Loading.Instance.CarrierA通知可测量)
                            {
                                Common.Data_A_Measure1 = Common.CloneTempData(ref Common.Data_A_Load);

                                isMaterialFromA = true;
                                Loading.Instance.CarrierA通知可测量 = false;
                                Machine.do_激光控制脉冲切换.OFF();
                                Step = StationStep.长边Or短边先;
                            }
                            else if (Loading.Instance.CarrierB通知可测量)
                            {
                                Common.Data_B_Measure1 = Common.CloneTempData(ref Common.Data_B_Load);

                                isMaterialFromA = false;
                                Loading.Instance.CarrierB通知可测量 = false;
                                Machine.do_激光控制脉冲切换.ON();
                                Step = StationStep.长边Or短边先;
                            }
                        }
                        break;
                    case StationStep.长边Or短边先://无区分方便改代码
                        if (isMaterialFromA)
                        {
                            Carrier.Instance.CarrierA.Location = CarrierLocation.Measure;
                            SendList_CarrierA.ForEach(x => x.SetBarcode($"Barcode{Common.Count_CarrierA}"));
                            Step = StationStep.CarrierA长边1预移动起始点;
                        }
                        else
                        {
                            Carrier.Instance.CarrierB.Location = CarrierLocation.Measure;
                            SendList_CarrierB.ForEach(x => x.SetBarcode($"Barcode{Common.Count_CarrierB}"));
                            Step = StationStep.CarrierB长边1预移动起始点;
                        }
                        break;
                    #region CarrierA 激光扫描
                    case StationStep.CarrierA长边1预移动起始点:
                        #region A_长边1
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierA激光长边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Laser_长边1正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierA激光长边1正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierA长边1移动起始点完成;
                        break;
                    case StationStep.CarrierA长边1移动起始点完成:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光长边1正方向扫描起始位);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Laser_长边1正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光长边角度);
                        if (!axRes) break;
                        Step = StationStep.CarrierA长边1激光扫描;
                        break;
                    case StationStep.CarrierA长边1激光扫描:
                        if (激光组是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierA(ref Send_A_激光组长边1正方向);
                        激光组是否忙碌 = true;
                        Step = StationStep.CarrierA长边1激光扫描等待Ready;
                        break;
                    case StationStep.CarrierA长边1激光扫描等待Ready:
                        if (StationServer.FindReturnFitState(Send_A_激光组长边1正方向, ReturnState.Ready))
                        {
                            Step = StationStep.CarrierA长边1激光扫描等待OK;
                        }
                        break;
                    case StationStep.CarrierA长边1激光扫描等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_A_激光组长边1正方向, ReturnState.GrabEnd))
                                {
                                    激光组是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        Step = StationStep.CarrierA长边1移动终点;
                        break;
                    case StationStep.CarrierA长边1移动终点:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光长边1正方向扫描终点位);
                        if (!axRes) break;
                        #endregion
                        Step = StationStep.CarrierA长边2预移动起始点;
                        break;
                    case StationStep.CarrierA长边2预移动起始点:
                        #region A_长边2
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierA激光长边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Laser_长边2正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierA激光长边2正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierA长边2移动起始点完成;
                        break;
                    case StationStep.CarrierA长边2移动起始点完成:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光长边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Laser_长边2正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光长边2正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierA长边2激光扫描;
                        break;
                    case StationStep.CarrierA长边2激光扫描:
                        if (激光组是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierA(ref Send_A_激光组长边2正方向);
                        激光组是否忙碌 = true;
                        Step = StationStep.CarrierA长边2激光扫描等待Ready;
                        break;
                    case StationStep.CarrierA长边2激光扫描等待Ready:
                        if (StationServer.FindReturnFitState(Send_A_激光组长边2正方向, ReturnState.Ready))
                        {
                            Step = StationStep.CarrierA长边2激光扫描等待OK;
                        }
                        break;
                    case StationStep.CarrierA长边2激光扫描等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_A_激光组长边2正方向, ReturnState.GrabEnd))
                                {
                                    激光组是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        Step = StationStep.CarrierA长边2移动终点;
                        break;
                    case StationStep.CarrierA长边2移动终点:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光长边2正方向扫描终点位);
                        if (!axRes) break;
                        #endregion
                        Step = StationStep.CarrierA短边1预移动起始点;
                        break;
                    case StationStep.CarrierA短边1预移动起始点:
                        #region 短边1
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Laser_短边1正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierA激光短边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierA激光短边1正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierA短边1移动起始点完成;
                        break;
                    case StationStep.CarrierA短边1移动起始点完成:
                        axRes = MotionControl.AxesMove(Machine.pos_Laser_短边1正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光短边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierA激光短边1正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierA短边1激光扫描;
                        break;
                    case StationStep.CarrierA短边1激光扫描:
                        if (激光组是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierA(ref Send_A_激光组短边1正方向);
                        激光组是否忙碌 = true;
                        Step = StationStep.CarrierA短边1激光扫描等待Ready;
                        break;
                    case StationStep.CarrierA短边1激光扫描等待Ready:
                        if (StationServer.FindReturnFitState(Send_A_激光组短边1正方向, ReturnState.Ready))
                        {
                            Step = StationStep.CarrierA短边1激光扫描等待OK;
                        }
                        break;
                    case StationStep.CarrierA短边1激光扫描等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_A_激光组短边1正方向, ReturnState.GrabEnd))
                                {
                                    激光组是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        Step = StationStep.CarrierA短边1移动终点;
                        break;
                    case StationStep.CarrierA短边1移动终点:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光短边1正方向扫描终点位);
                        if (!axRes) break;
                        #endregion
                        Step = StationStep.CarrierA短边2预移动起始点;
                        break;
                    case StationStep.CarrierA短边2预移动起始点:
                        #region 短边2
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Laser_短边2正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierA激光短边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierA激光短边2正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierA短边2移动起始点完成;
                        break;
                    case StationStep.CarrierA短边2移动起始点完成:
                        axRes = MotionControl.AxesMove(Machine.pos_Laser_短边2正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光短边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierA激光短边2正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierA短边2激光扫描;
                        break;
                    case StationStep.CarrierA短边2激光扫描:
                        if (激光组是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierA(ref Send_A_激光组短边2正方向);
                        激光组是否忙碌 = true;
                        Step = StationStep.CarrierA短边2激光扫描等待Ready;
                        break;
                    case StationStep.CarrierA短边2激光扫描等待Ready:
                        if (StationServer.FindReturnFitState(Send_A_激光组短边2正方向, ReturnState.Ready))
                        {
                            Step = StationStep.CarrierA短边2激光扫描等待OK;
                        }
                        break;
                    case StationStep.CarrierA短边2激光扫描等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_A_激光组短边2正方向, ReturnState.GrabEnd))
                                {
                                    激光组是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        Step = StationStep.CarrierA短边2移动终点;
                        break;
                    case StationStep.CarrierA短边2移动终点:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光短边2正方向扫描终点位);
                        if (!axRes) break;
                        #endregion
                        Step = StationStep.是否允许CarrierA到R角扫描;
                        break;
                    case StationStep.是否允许CarrierA到R角扫描:
                        if (是否允许载具到R角激光扫描位)
                        {
                            是否允许载具到R角激光扫描位 = false;
                            Step = StationStep.CarrierAR角预移动起始点;
                        }
                        break;
                    case StationStep.CarrierAR角预移动起始点:
                        #region A_R角
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Laser_R角正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierA激光R角角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光R角正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierAR角移动起始点完成;
                        break;
                    case StationStep.CarrierAR角移动起始点完成:
                        axRes = MotionControl.AxesMove(Machine.pos_Laser_R角正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光R角角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光R角正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierAR角激光扫描;
                        break;
                    case StationStep.CarrierAR角激光扫描:
                        if (激光组是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierA(ref Send_A_激光组R角正方向);
                        激光组是否忙碌 = true;
                        Step = StationStep.CarrierAR角激光扫描等待Ready;
                        break;
                    case StationStep.CarrierAR角激光扫描等待Ready:
                        if (StationServer.FindReturnFitState(Send_A_激光组R角正方向, ReturnState.Ready))
                        {
                            Step = StationStep.CarrierAR角激光扫描等待OK;
                        }
                        break;
                    case StationStep.CarrierAR角激光扫描等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_A_激光组R角正方向, ReturnState.GrabEnd))
                                {
                                    激光组是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        Step = StationStep.CarrierAR角移动终点;
                        break;
                    case StationStep.CarrierAR角移动终点:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA激光R角正方向扫描终点位);
                        if (!axRes) break;
                        #endregion
                        Step = StationStep.CarrierA移动上相机1拍照位;
                        break;
                    case StationStep.CarrierA移动上相机1拍照位:
                        #region A_上相机1
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA相机拍照位1);
                        if (!axRes) break;
                        Step = StationStep.CarrierA上相机1;
                        break;
                    case StationStep.允许CarrierB到测量位:
                        是否允许载具到R角激光扫描位 = true;
                        通知上料可到测量位 = true;
                        break;
                    case StationStep.CarrierA上相机1:
                        if (上相机是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierA(ref Send_A_上相机1);
                        上相机是否忙碌 = true;
                        Step = StationStep.CarrierA上相机1等待Ready;
                        break;
                    case StationStep.CarrierA上相机1等待Ready:
                        if (StationServer.FindReturnFitState(Send_A_上相机1, ReturnState.Ready))
                        {
                            Step = StationStep.CarrierA上相机1等待OK;
                        }
                        break;
                    case StationStep.CarrierA上相机1等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_A_上相机1, ReturnState.GrabEnd))
                                {
                                    上相机是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        #endregion
                        Step = StationStep.CarrierA移动上相机2拍照位;
                        break;
                    case StationStep.CarrierA移动上相机2拍照位:
                        #region A_上相机2
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA相机拍照位2);
                        if (!axRes) break;
                        Step = StationStep.CarrierA上相机2;
                        break;
                    case StationStep.CarrierA上相机2:
                        if (上相机是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierA(ref Send_A_上相机2);
                        上相机是否忙碌 = true;
                        Step = StationStep.CarrierA上相机2等待Ready;
                        break;
                    case StationStep.CarrierA上相机2等待Ready:
                        if (StationServer.FindReturnFitState(Send_A_上相机2, ReturnState.Ready))
                        {
                            Step = StationStep.CarrierA上相机2等待OK;
                        }
                        break;
                    case StationStep.CarrierA上相机2等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_A_上相机2, ReturnState.GrabEnd))
                                {
                                    上相机是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        #endregion
                        Step = StationStep.是否允许移动到下料;
                        break;
                    #endregion

                    #region CarrierB 激光扫描
                    case StationStep.CarrierB长边1预移动起始点:
                        #region B_长边1
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierB激光长边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Laser_长边1正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierB激光长边1正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierB长边1移动起始点完成;
                        break;
                    case StationStep.CarrierB长边1移动起始点完成:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光长边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Laser_长边1正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光长边1正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierB长边1激光扫描;
                        break;
                    case StationStep.CarrierB长边1激光扫描:
                        if (激光组是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierB(ref Send_B_激光组长边1正方向);
                        激光组是否忙碌 = true;
                        Step = StationStep.CarrierB长边1激光扫描等待Ready;
                        break;
                    case StationStep.CarrierB长边1激光扫描等待Ready:
                        if (StationServer.FindReturnFitState(Send_B_激光组长边1正方向, ReturnState.Ready, false))
                        {
                            Step = StationStep.CarrierB长边1激光扫描等待OK;
                        }
                        break;
                    case StationStep.CarrierB长边1激光扫描等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_B_激光组长边1正方向, ReturnState.GrabEnd, false))
                                {
                                    激光组是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        Step = StationStep.CarrierB长边1移动终点;
                        break;
                    case StationStep.CarrierB长边1移动终点:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光长边1正方向扫描终点位);
                        if (!axRes) break;
                        #endregion
                        Step = StationStep.CarrierB长边2预移动起始点;
                        break;
                    case StationStep.CarrierB长边2预移动起始点:
                        #region B_长边2
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierB激光长边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Laser_长边2正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierB激光长边2正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierB长边2移动起始点完成;
                        break;
                    case StationStep.CarrierB长边2移动起始点完成:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光长边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Laser_长边2正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光长边2正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierB长边2激光扫描;
                        break;
                    case StationStep.CarrierB长边2激光扫描:
                        if (激光组是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierB(ref Send_B_激光组长边2正方向);
                        激光组是否忙碌 = true;
                        Step = StationStep.CarrierB长边2激光扫描等待Ready;
                        break;
                    case StationStep.CarrierB长边2激光扫描等待Ready:
                        if (StationServer.FindReturnFitState(Send_B_激光组长边2正方向, ReturnState.Ready, false))
                        {
                            Step = StationStep.CarrierB长边2激光扫描等待OK;
                        }
                        break;
                    case StationStep.CarrierB长边2激光扫描等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_B_激光组长边2正方向, ReturnState.GrabEnd, false))
                                {
                                    激光组是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        Step = StationStep.CarrierB长边2移动终点;
                        break;
                    case StationStep.CarrierB长边2移动终点:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光长边2正方向扫描终点位);
                        if (!axRes) break;
                        #endregion
                        Step = StationStep.CarrierB短边1预移动起始点;
                        break;
                    case StationStep.CarrierB短边1预移动起始点:
                        #region B_短边1
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierB激光短边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Laser_短边1正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierB激光短边1正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierB短边1移动起始点完成;
                        break;
                    case StationStep.CarrierB短边1移动起始点完成:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光短边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Laser_短边1正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光短边1正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierB短边1激光扫描;
                        break;
                    case StationStep.CarrierB短边1激光扫描:
                        if (激光组是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierB(ref Send_B_激光组短边1正方向);
                        激光组是否忙碌 = true;
                        Step = StationStep.CarrierB短边1激光扫描等待Ready;
                        break;
                    case StationStep.CarrierB短边1激光扫描等待Ready:
                        if (StationServer.FindReturnFitState(Send_B_激光组短边1正方向, ReturnState.Ready, false))
                        {
                            Step = StationStep.CarrierB短边1激光扫描等待OK;
                        }
                        break;
                    case StationStep.CarrierB短边1激光扫描等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_B_激光组短边1正方向, ReturnState.GrabEnd, false))
                                {
                                    激光组是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        Step = StationStep.CarrierB短边1移动终点;
                        break;
                    case StationStep.CarrierB短边1移动终点:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光短边1正方向扫描终点位);
                        if (!axRes) break;
                        #endregion
                        Step = StationStep.CarrierB短边2预移动起始点;
                        break;
                    case StationStep.CarrierB短边2预移动起始点:
                        #region B_短边2
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierB激光短边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Laser_短边2正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierB激光短边2正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierB短边2移动起始点完成;
                        break;
                    case StationStep.CarrierB短边2移动起始点完成:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光短边角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Laser_短边2正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光短边2正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierB短边2激光扫描;
                        break;
                    case StationStep.CarrierB短边2激光扫描:
                        if (激光组是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierB(ref Send_B_激光组短边2正方向);
                        激光组是否忙碌 = true;
                        Step = StationStep.CarrierB短边2激光扫描等待Ready;
                        break;
                    case StationStep.CarrierB短边2激光扫描等待Ready:
                        if (StationServer.FindReturnFitState(Send_B_激光组短边2正方向, ReturnState.Ready, false))
                        {
                            Step = StationStep.CarrierB短边2激光扫描等待OK;
                        }
                        break;
                    case StationStep.CarrierB短边2激光扫描等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_B_激光组短边2正方向, ReturnState.GrabEnd, false))
                                {
                                    激光组是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        Step = StationStep.CarrierB短边2移动终点;
                        break;
                    case StationStep.CarrierB短边2移动终点:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光短边2正方向扫描终点位);
                        if (!axRes) break;
                        #endregion
                        Step = StationStep.CarrierBR角预移动起始点;
                        break;
                    case StationStep.CarrierBR角预移动起始点:
                        #region B_R角 
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierB激光R角角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Laser_R角正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMoveNoWait(Machine.pos_Measure1_CarrierB激光R角正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierBR角移动起始点完成;
                        break;
                    case StationStep.CarrierBR角移动起始点完成:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光R角角度);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Laser_R角正方向);
                        if (!axRes) break;
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光R角正方向扫描起始位);
                        if (!axRes) break;
                        Step = StationStep.CarrierBR角激光扫描;
                        break;
                    case StationStep.CarrierBR角激光扫描:
                        if (激光组是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierB(ref Send_B_激光组R角正方向);
                        激光组是否忙碌 = true;
                        Step = StationStep.CarrierBR角激光扫描等待Ready;
                        break;
                    case StationStep.CarrierBR角激光扫描等待Ready:
                        if (StationServer.FindReturnFitState(Send_B_激光组R角正方向, ReturnState.Ready, false))
                        {
                            Step = StationStep.CarrierBR角激光扫描等待OK;
                        }
                        break;
                    case StationStep.CarrierBR角激光扫描等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_B_激光组R角正方向, ReturnState.GrabEnd, false))
                                {
                                    激光组是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        Step = StationStep.CarrierBR角移动终点;
                        break;
                    case StationStep.CarrierBR角移动终点:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB激光R角正方向扫描终点位);
                        if (!axRes) break;
                        #endregion
                        Step = StationStep.CarrierB移动上相机1拍照位;
                        break;
                    case StationStep.CarrierB移动上相机1拍照位:
                        #region B_上相机1
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB相机拍照位1);
                        if (!axRes) break;
                        Step = StationStep.CarrierB上相机1;
                        break;
                    case StationStep.CarrierB上相机1:
                        if (上相机是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierB(ref Send_B_上相机1);
                        上相机是否忙碌 = true;
                        Step = StationStep.CarrierB上相机1等待Ready;
                        break;
                    case StationStep.CarrierB上相机1等待Ready:
                        if (StationServer.FindReturnFitState(Send_B_上相机1, ReturnState.Ready, false))
                        {
                            Step = StationStep.CarrierB上相机1等待OK;
                        }
                        break;
                    case StationStep.CarrierB上相机1等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_B_上相机1, ReturnState.GrabEnd, false))
                                {
                                    上相机是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        #endregion
                        Step = StationStep.CarrierB移动上相机2拍照位;
                        break;
                    case StationStep.CarrierB移动上相机2拍照位:
                        #region B_上相机2
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB相机拍照位2);
                        if (!axRes) break;
                        Step = StationStep.CarrierB上相机2;
                        break;
                    case StationStep.CarrierB上相机2:
                        if (上相机是否忙碌)
                        {
                            break;
                        }
                        StationServer.SendStr_CarrierB(ref Send_B_上相机2);
                        上相机是否忙碌 = true;
                        Step = StationStep.CarrierB上相机2等待Ready;
                        break;
                    case StationStep.CarrierB上相机2等待Ready:
                        if (StationServer.FindReturnFitState(Send_B_上相机2, ReturnState.Ready, false))
                        {
                            Step = StationStep.CarrierB上相机2等待OK;
                        }
                        break;
                    case StationStep.CarrierB上相机2等待OK:
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                if (StationServer.FindReturnFitState(Send_B_上相机2, ReturnState.GrabEnd, false))
                                {
                                    上相机是否忙碌 = false;
                                    break;
                                }
                                Thread.Sleep(10);
                            }
                        });
                        #endregion
                        Step = StationStep.是否允许移动到下料;
                        break;
                    #endregion

                    case StationStep.是否允许移动到下料:
                        if (CarrierUnloadTrans.Instance.允许测量1移动到下料位置)
                        {
                            CarrierUnloadTrans.Instance.允许测量1移动到下料位置 = false;

                            if (isMaterialFromA)
                                Step = StationStep.CarrierA移动到下料;
                            else
                                Step = StationStep.CarrierB移动到下料;
                        }
                        break;
                    case StationStep.CarrierA移动到下料:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierA放料位);
                        if (!axRes) break;
                        通知中转CarrierA可下料 = true;
                        Step = StationStep.End;
                        break;
                    case StationStep.CarrierB移动到下料:
                        axRes = MotionControl.AxesMove(Machine.pos_Measure1_CarrierB放料位);
                        if (!axRes) break;
                        通知中转CarrierB可下料 = true;
                        Step = StationStep.End;
                        break;
                    case StationStep.End:
                        logCtStr += $"{logBaseInfo},Measure1结束,{swStepTime.ElapsedMilliseconds},{Environment.NewLine}";
                        logCtStr += $"{logBaseInfo},Measure1结束,{swStationCT.ElapsedMilliseconds},{Environment.NewLine}";
                        PublicMethod.WriteLogToTxt(logCtStr, $"000StepCT_{DateTime.Now.ToString("yyyyMMdd")}", "2.Measure1");
                        Step = StationStep.WaitMacInit;
                        break;

                }

            }
            catch (Exception ex)
            {
                UILog_Error($"[ Loading Thread ERROR] {ex.Message}");
            }
        }

        public override void GRRActionProcess()
        {

        }

        /// <summary>
        /// 工位步骤定义
        /// </summary>
        public enum StationStep
        {
            /// <summary>
            /// 等待机台初始化
            /// </summary>
            [Description("等待机台初始化")]
            WaitMacInit,
            [Description("开始")]
            Start,
            等待通知上料完成,
            长边Or短边先,
            //A_长边1
            CarrierA长边1预移动起始点,
            CarrierA长边1移动起始点完成,
            CarrierA长边1激光扫描,
            CarrierA长边1激光扫描等待Ready,
            CarrierA长边1激光扫描等待OK,
            CarrierA长边1移动终点,
            //A_长边2
            CarrierA长边2预移动起始点,
            CarrierA长边2移动起始点完成,
            CarrierA长边2激光扫描,
            CarrierA长边2激光扫描等待Ready,
            CarrierA长边2激光扫描等待OK,
            CarrierA长边2移动终点,
            //A_短边1
            CarrierA短边1预移动起始点,
            CarrierA短边1移动起始点完成,
            CarrierA短边1激光扫描,
            CarrierA短边1激光扫描等待Ready,
            CarrierA短边1激光扫描等待OK,
            CarrierA短边1移动终点,
            //A_短边2
            CarrierA短边2预移动起始点,
            CarrierA短边2移动起始点完成,
            CarrierA短边2激光扫描,
            CarrierA短边2激光扫描等待Ready,
            CarrierA短边2激光扫描等待OK,
            CarrierA短边2移动终点,
            //
            是否允许CarrierA到R角扫描,
            //A_R角
            CarrierAR角预移动起始点,
            CarrierAR角移动起始点完成,
            CarrierAR角激光扫描,
            CarrierAR角激光扫描等待Ready,
            CarrierAR角激光扫描等待OK,
            CarrierAR角移动终点,
            //A_上相机
            CarrierA移动上相机1拍照位,
            允许CarrierB到测量位,
            CarrierA上相机1,
            CarrierA上相机1等待Ready,
            CarrierA上相机1等待OK,
            CarrierA移动上相机2拍照位,
            CarrierA上相机2,
            CarrierA上相机2等待Ready,
            CarrierA上相机2等待OK,

            //B_长边1
            CarrierB长边1预移动起始点,
            CarrierB长边1移动起始点完成,
            CarrierB长边1激光扫描,
            CarrierB长边1激光扫描等待Ready,
            CarrierB长边1激光扫描等待OK,
            CarrierB长边1移动终点,
            //B_长边2
            CarrierB长边2预移动起始点,
            CarrierB长边2移动起始点完成,
            CarrierB长边2激光扫描,
            CarrierB长边2激光扫描等待Ready,
            CarrierB长边2激光扫描等待OK,
            CarrierB长边2移动终点,
            //B_短边1
            CarrierB短边1预移动起始点,
            CarrierB短边1移动起始点完成,
            CarrierB短边1激光扫描,
            CarrierB短边1激光扫描等待Ready,
            CarrierB短边1激光扫描等待OK,
            CarrierB短边1移动终点,
            //B_短边2
            CarrierB短边2预移动起始点,
            CarrierB短边2移动起始点完成,
            CarrierB短边2激光扫描,
            CarrierB短边2激光扫描等待Ready,
            CarrierB短边2激光扫描等待OK,
            CarrierB短边2移动终点,
            //
            是否允许CarrierB到R角扫描,
            //B_R角
            CarrierBR角预移动起始点,
            CarrierBR角移动起始点完成,
            CarrierBR角激光扫描,
            CarrierBR角激光扫描等待Ready,
            CarrierBR角激光扫描等待OK,
            CarrierBR角移动终点,
            //B_上相机
            CarrierB移动上相机1拍照位,
            允许CarrierA到测量位,
            CarrierB上相机1,
            CarrierB上相机1等待Ready,
            CarrierB上相机1等待OK,
            CarrierB移动上相机2拍照位,
            CarrierB上相机2,
            CarrierB上相机2等待Ready,
            CarrierB上相机2等待OK,
            //下料
            是否允许移动到下料,
            CarrierA移动到下料,
            CarrierB移动到下料,
            [Description("工站完成")]
            End,
        }

    }
}
